<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogApply.visible"
    :width="900"
    :title="dialogApply.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-form
      ref="addOrEditFormRef"
      v-loading="loading"
      :model="formData"
      :rules="rules"
      label-width="160"
      size=""
      style="padding-top: 20px"
    >
      <el-row justify="start" class="d_left">
        <el-col :span="12" class="">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="formData.projectName" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="项目编号" prop="projectNum">
            <el-input v-model="formData.projectNum" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="缴纳主体名称" prop="paymentName">
            <el-input v-model="formData.paymentName" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="缴纳主体统一社会信用代码" prop="paymentCode">
            <el-input v-model="formData.paymentCode" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="账户编号" prop="guaranteeAccountCode">
            <el-input v-model="formData.guaranteeAccountCode" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="可退还金额" prop="paid">
            <el-input v-model="formData.paid" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="退还金额" prop="applyAmount">
            <el-input v-model="formData.applyAmount" type="number" placeholder="请输入退还金额" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="">
          <el-form-item label="申请理由" prop="applyReason">
            <el-input
              v-model="formData.applyReason"
              type="textarea"
              style="width: 100%"
              :row="3"
              placeholder="请说明申请退还的理由"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogApply.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ProjectGuaranteeRefundEditForm } from "@/api/guarantee/types";
import { GetProjectGuaranteeRefund, ApplyProjectGuaranteeRefund } from "@/api/guarantee/index";
import { FormRules } from "element-plus";

const props = defineProps({
  dialogApply: {
    type: Object,
    default: () => ({
      visible: false,
      title: "申请退还",
      footBtn: "确认申请",
      id: 0,
    }),
  },
});

const { dialogApply } = toRefs(props);

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
const detailInfo = ref<any>({});
const formData = reactive<ProjectGuaranteeRefundEditForm>({});
const addOrEditFormRef = ref(ElForm);

const rules = reactive<FormRules<ProjectGuaranteeRefundEditForm>>({
  applyAmount: [{ required: true, message: "请输入退还金额", trigger: "blur" }],
  applyReason: [{ required: true, message: "请输入申请理由", trigger: "blur" }],
});

const initForm = () => {
  loading.value = true;
  GetProjectGuaranteeRefund(0, dialogApply.value.id)
    .then((res) => {
      if (res.success) {
        Object.assign(formData, res.data);
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  initForm();
});

/**
 * 关闭弹窗
 */
function closeDialog(query: boolean) {
  emit("sendClose", query);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      ApplyProjectGuaranteeRefund(formData)
        .then((res) => {
          closeDialog(true);
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
